// 1.0 release lol - first one released XD yay!
jQuery(document).ready(function(){
	var link = null;
	// method used for saving data.. it will call over ajax request page on server
	// that page will do the rest and return jSON object...
	$j("#save").bind("click", function(){
		$j("#progress").removeClass("progress-error");
		$j("#progress").fadeIn("slow");
		$j("#notice").fadeOut("fast");
		$j("#progress #pmessage").text("Please wait... system is sending data...");
		
		var url = $j("#addimage_form").attr("action");
		var name = $j("#name").attr("value");
		var tags = $j("#tags").attr("value");
		var status = $j("#status").attr("value");
		var group = $j("#group").attr("value");
		var description = $j("#description").attr("value");
		
		$j("#name").css({
			backgroundColor: "white",
			color: "#8a8a8a",
			border: "1px #ccc solid"
		});

		$j("#tags").css({
			backgroundColor: "white",
			color: "#8a8a8a",
			border: "1px #ccc solid"
		});

		$j("#status").css({
			backgroundColor: "white",
			color: "#8a8a8a",
			border: "1px #ccc solid"
		});

		$j("#group").css({
			backgroundColor: "white",
			color: "#8a8a8a",
			border: "1px #ccc solid"
		});

		$j("#description").css({
			backgroundColor: "white",
			color: "#8a8a8a",
			border: "1px #ccc solid"
		});
						
		$j.ajax({
			type: "POST",
			url:  url,
			data: "name="+name+"&tags="+tags+"&status="+status+"&group="+group+"&description="+description,
			dataType: "json",
			success: function ( data, status ){
				
				if ( data.status == "false" )
				{
					$j("#progress").addClass("progress-error");
					$j("#progress #pmessage").text(data.message);
					$j("#"+data.input).css({
						backgroundColor: "#b70a33",
						color: "#fff",
						border: "1px #d80e3d solid"
					});	
				}
				else if ( data.status == "true" )
				{
					$j("#progress").removeClass("progress-error");
					$j("#progress #pmessage").text(data.message);
					
					if ( data.task == "finish" )
					{
						$j("notice_ok").attr("href", domain + data.location );
						link = domain + data.location;
						$j("#progress").fadeOut("fast");
						$j("#body").css({
							overflow: "hidden"	
						});
						window.scrollTo(0,0);
						$j("#notice").fadeIn("fast");
					}
				}
				
			}
		});
	});
	
	// on click show confirmation. if true, navigate to component root
	// else do nothing... just say that it's canceled!
	$j("#discard").bind("click", function(){
		
		var conf = confirm ( "Do you really want to discard adding new image?" );
		if ( conf )
		{
			$j("#progress #pmessage").text("Please wait...");
			$j("#progress").fadeIn("slow");
			setTimeout ( 'document.location="'+domain+'storage"', 3000 );	
		}
		else
		{
			$j("#progress #pmessage").text("Canceled...");
			$j("#progress").fadeIn("slow");
			setTimeout ( '$j("#progress").fadeOut("normal");', 3000 );	
		}
		
	});	
	
	// if ok than navigate to component root
	$j("#notice_ok").bind("click", function(){
			
			$j("#notice").fadeOut("normal");
			$j("#progress").fadeOut("fast");
			$j("#progress #pmessage").text("Please wait...");
			setTimeout ( 'document.location="'+link+'"', 3000 );	
	});	
	
	// if not, reload window so that old cash data go away. 		
	$j("#notice_not").bind("click", function(){
			$j("#notice").fadeOut("normal");
			$j("#progress").fadeOut("fast");
			location.reload(true);
	});	
	
	// if close, reload window so that old cash data go away. 
	$j("#close").bind("click", function(){
			$j("#notice").fadeOut("normal");
			$j("#progress").fadeOut("fast");
			location.reload(true);
	});	
	
});